package com.eastedu.report.controller;


import com.eastedu.common.page.PageResult;
import com.eastedu.common.page.Pageable;
import com.eastedu.report.common.model.po.TemplateConfigPO;
import com.eastedu.report.common.model.vo.ReportTemplateUploadVO;
import com.eastedu.report.template.ReportTemplateConfiguration;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 模版管理
 *
 * @author luozhenzhong
 */
@RestController
@RequestMapping(value = "/api/templates", params = {"version=1.0"})
@Tag(name = "报告模版管理 - 1.0", description = "报告模版管理 - 1.0")
@Slf4j
public class ReportTemplateConfigController {

    private final ReportTemplateConfiguration reportTemplateConfiguration;

    /**
     * Instantiates a new Report template config controller.
     *
     * @param reportTemplateConfiguration the report template configuration
     */
    public ReportTemplateConfigController(ReportTemplateConfiguration reportTemplateConfiguration) {
        this.reportTemplateConfiguration = reportTemplateConfiguration;
    }

    /**
     * List http entity.
     *
     * @param page the page
     * @return the http entity
     */
    @Operation(summary = "模版列表", description = "模版列表")
    @GetMapping
    public List<TemplateConfigPO> list(Pageable page) {
        return reportTemplateConfiguration.list(page).getItems();
    }

    /**
     * List by page http entity.
     *
     * @param page the page
     * @return the http entity
     */
    @Operation(summary = "分页模版列表", description = "分页模版列表")
    @GetMapping("page")
    public PageResult<TemplateConfigPO> listByPage(Pageable page) {
        return reportTemplateConfiguration.list(page);
    }

    /**
     * Add http entity.
     *
     * @param vo the vo
     * @throws IOException the io exception
     */
    @Operation(summary = "添加模版-表单提交方式", description = "添加模版-表单提交方式")
    @PostMapping("")
    public void add(@Validated @ModelAttribute("template") ReportTemplateUploadVO vo) throws IOException {
        reportTemplateConfiguration.add(vo);
    }


    /**
     * Update http entity.
     *
     * @param templateId the template id
     * @param file       the file
     * @throws IOException the io exception
     */
    @Operation(summary = "修改模版", description = "修改模版")
    @PutMapping("{templateId}")
    public void update(@Parameter(description = "模版ID", required = true) @PathVariable Long templateId,
                       @Parameter(description = "模版文件", required = true) @RequestParam("file") MultipartFile file) throws IOException {
        reportTemplateConfiguration.update(templateId, file);
    }

    /**
     * Delete http entity.
     *
     * @param templateId the template id
     */
    @Operation(summary = "删除模版", description = "删除模版")
    @DeleteMapping("{templateId}")
    public void delete(@Parameter(description = "模版ID", required = true) @PathVariable Long templateId) {
        reportTemplateConfiguration.delete(templateId);
    }

    /**
     * Get.
     *
     * @param templateId the template id
     * @param response   the response
     * @throws IOException the io exception
     */
    @Operation(summary = "下载模版", description = "下载模版")
    @GetMapping("{templateId}")
    public void get(@Parameter(description = "模版ID", required = true) @PathVariable Long templateId, HttpServletResponse response) throws IOException {
        TemplateConfigPO po = reportTemplateConfiguration.get(templateId);

        // 设置相关格式
        response.setHeader("Content-Type", "application/octet-stream;charset=utf-8");
        // 设置下载后的文件名以及header
        response.addHeader("Content-disposition", "attachment;fileName=" + new String(po.getOriginalFileName().getBytes(StandardCharsets.UTF_8), "ISO8859-1"));
        ServletOutputStream outputStream = response.getOutputStream();

        BufferedInputStream inputStream = new BufferedInputStream(new ByteArrayInputStream(po.getContent()));
        int a;
        while ((a = inputStream.read()) != -1) {
            outputStream.write(a);
        }
        outputStream.flush();
        outputStream.close();
    }


}
